<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:shiro="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<style>
    .bootstrap-table,
    .myFull {
        width: 100%;
        height: 100%;
    }

    .fixed-table-toolbar {
        height: 0px;
    }

    .fixed-table-container {
        height: calc(100% - 75px);
    }

    .fixed-table-pagination {
        height: 55px;
    }
</style>
<input id="myId" th:value="${null!=myId?myId:''}" type="hidden">
<div class="wrapper wrapper-content myFull">
    <div class="col-sm-12 nopadding myFull">
        <div class="ibox myFull">
            <div class="ibox-body myFull">
                <div class="col-sm-12 "
                     style="height: 70px;background-color: #fff; display: flex; align-items: center;">
                    <div style="margin-right: 5px;">申报单位:</div>
                    <div class="columns pull-left col-md-1 nopadding" style="width: 150px;">
                        <input id="searchName1" type="text" class="form-control"
                               placeholder="请输入单位名称">
                    </div>
                    <div style="margin: 0px 5px 0px 20px;">联系人:</div>
                    <div class="columns pull-left col-md-1 nopadding" style="width: 150px;">
                        <input id="searchName2" type="text" class="form-control"
                               placeholder="请输入姓名">
                    </div>
                    <div style="margin: 0px 5px 0px 20px;">联系方式:</div>
                    <div class="columns pull-left col-md-1 nopadding" style="width: 150px;">
                        <input id="searchName3" type="text" class="form-control"
                               placeholder="请输入电话">
                    </div>
                    <div style="margin: 0px 5px 0px 20px;">时间:</div>
                    <div class="columns pull-left col-md-1 nopadding" style="width: 150px;">
                        <input id="searchName4" type="text" class="form-control"
                               placeholder="请选择时间">
                    </div>
                    <div class="columns pull-left" style="margin: 0px 20px;">
                        <button class="btn btn-success" onclick="reLoad()">查询</button>
                    </div>

                </div>
                <div class="fixed-table-toolbar col-sm-12"
                     style="height: 70px;background-color: #fff; display: flex; align-items: center;margin-top: 20px;">
                    <div class="columns pull-left">
                        <button type="button" class="btn  btn-primary" onclick="myExportALl()">
                            分类资料导出
                        </button>
                        <button type="button" class="btn  btn-primary" onclick="myExportALlTop()">
                            分类数据导出
                        </button>
                    </div>
                </div>
                <div class="col-sm-12 "
                     style="height: calc(100% - 160px);background-color: #fff; display: flex; align-items: center;">
                    <table id="exampleTable" data-mobile-responsive="true">
                    </table>
                </div>

            </div>
        </div>
    </div>
</div>


</div>
<div th:include="include :: footer"></div>
<script>
    let myPageSize = 10
    $(function () {
        layui.use('laydate', function() {
            var laydate = layui.laydate;
            laydate.render({
                elem: '#searchName4',
                type: 'year',
                value: '2023'
            });
            load();
        })
    });

    function load() {
        $('#exampleTable')
            .bootstrapTable(
                {
                    method: 'get', // 服务器数据的请求方式 get or post
                    url: "/jzweb/declare/list", // 服务器数据的加载地址
                    //	showRefresh : true,
                    //	showToggle : true,
                    //	showColumns : true,
                    iconSize: 'outline',
                    toolbar: '#exampleToolbar',
                    striped: true, // 设置为true会有隔行变色效果
                    dataType: "json", // 服务器返回的数据类型
                    pagination: true, // 设置为true会在底部显示分页条
                    singleSelect: false, // 设置为true将禁止多选
                    contentType: 'application/x-www-form-urlencoded;charset=utf-8',
                    // //发送到服务器的数据编码类型
                    pageSize: myPageSize, // 如果设置了分页，每页数据条数
                    pageNumber: 1, // 如果设置了分布，首页页码
                    //search : true, // 是否显示搜索框
                    showColumns: false, // 是否显示内容下拉框（选择显示的列）
                    sidePagination: "client", // 设置在哪里进行分页，可选值为"client" 或者 "server"
                    queryParamsType: "",
                    // //设置为limit则会发送符合RESTFull格式的参数
                    queryParams: function (params) {
                        myPageSize = Number(params.pageSize)
                        return {
                            //说明：传入后台的参数包括offset开始索引，limit步长，sort排序列，order：desc或者,以及所有列的键值对
                            current: params.pageNumber,
                            size: params.pageSize,
                            orgName: $("#searchName1").val(),
                            contacts: $("#searchName2").val(),
                            phone: $("#searchName3").val(),
                            createrDate: $("#searchName4").val()+'-01-01',
                            id: $("#myId").val()
                        };
                    },
                    // //请求服务器数据时，你可以通过重写参数的方式添加一些额外的参数，例如 toolbar 中的参数 如果
                    // queryParamsType = 'limit' ,返回参数必须包含
                    // limit, offset, search, sort, order 否则, 需要包含:
                    // pageSize, pageNumber, searchText, sortName,
                    // sortOrder.
                    // 返回false将会终止请求
                    responseHandler: function (res) {
                        console.log('responseHandleasasr1111',res)
                        $("#exampleTable").bootstrapTable("load", res.data);
                        return false;
                    },
                    columns: [
                        {
                            checkbox: true
                        },
                        {
                            field: 'orgName',
                            title: '申报单位'
                        },
                        {
                            field: 'contacts',
                            title: '联系人'
                        },
                        {
                            field: 'place',
                            title: '职位'
                        },
                        {
                            field: 'position',
                            title: '职称'
                        },
                        {
                            field: 'phone',
                            title: '联系方式'
                        },
                        {
                            field: 'province',
                            title: '所属省份'
                        },
                        {
                            field: 'city',
                            title: '所属市'
                        },
                        {
                            field: 'createrDate',
                            title: '申报时间'
                        },
                        {
                            field: 'status',
                            title: '审核状态',
                            formatter: function (value, row, index) {
                                //0 = 未审核，1 = 审核通过 2 = 审核拒绝
                                return row.status == 0 ? `<span className="label label-default">未审核</span>` :
                                    row.status == 1 ? `<span className="label label-success">审核通过</span>` :
                                        `<span className="label label-warning">审核拒绝</span>`;
                            }
                        },
                        {
                            field: 'remark',
                            title: '备注'
                        },
                        {
                            title: '操作',
                            field: 'id',
                            align: 'center',
                            formatter: function (value, row, index) {
                                var a = '<a class="btn btn-primary btn-sm" href="#" style="margin-right: 5px" mce_href="#" onclick="infors(\''
                                    + row.id
                                    + '\')">详情</a>';
                                var b = '<a class="btn btn-success btn-sm " style="margin-right: 5px" href="#" mce_href="#" onclick="myExport(\''
                                    + row.id
                                    + '\')">导出</a>';
                                var c = '<a class="btn btn-warning btn-sm " style="margin-right: 5px" href="#" title="删除"  mce_href="#" onclick="remove(\''
                                    + row.id
                                    + '\')">删除</a>';
                                return a + b + c;
                            }
                        }]
                });
    }

    function remove(id) {
        layer.confirm('确定要删除选中的记录？', {
            btn : [ '确定', '取消' ]
        }, function() {
            $.ajax({
                url : "/jzweb/declare/remove",
                type : "post",
                data : {
                    'id' : id
                },
                success : function(r) {
                    if (r.code==0) {
                        layer.msg(r.msg);

                    }else{
                        layer.msg(r.msg);
                    }
                    reLoad();
                },
                error:()=>{
                    reLoad();
                }
            });
        })
    }


    function reLoad() {
        $('#exampleTable').bootstrapTable('refresh');
    }


    const myExportALl = () => {
        var ids = $('#exampleTable').bootstrapTable('getSelections');
        let idList = []

        if (ids.length == 0) {
            layer.msg("请至少选择一份资料，或者选择全部资料")
            return;
        }
        if (myPageSize == ids.length) {
            console.log("全选")
        } else {
            ids.forEach(item => {
                idList.push(item.id)
            })
            console.log("单独")
        }
        var loading1 =  layer.open({
            type: 1,
            area : [ '500px', '400px' ],
            resize: false,
            shadeClose: true,
            title: '分类导出选择条件',
            content: ` <div class="layui-form" style="padding: 20px;">
                                <div class="layui-form-item" style="height: 250px;display: flex;align-items: center;">
                                    <label class="layui-form-label" style="width: 100px;">数据类型</label>
                                      <div class="layui-input-inline " style="width: 350px;">
                                        <select id="dataType" class="layui-input" value="" placeholder="请选择数据类型">
                                        <option value="">全部</option>
                                        <option value="1">实习单位</option>
                                        <option value="2">优秀培训教师</option>
                                        <option value="3">优秀考务工作者</option>
                                        <option value="4">优秀组织管理奖</option>
                                        <option value="5">优秀案例</option>
                                        </select>
                                    </div>
                                </div>
                                 <div class="layui-form-item"> <button class="layui-btn layui-btn-fluid" id="btn2" >确定</button>
                                 </div>
                           </div>`,
            success: function(){
                layui.use('form', function(){
                    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                });
                $('#btn2').click(function(){
                    var loading = layer.load('加载中...');
                    //调接口
                    $.ajax({
                        cache: true,
                        type: "POST",
                        url: "/jzweb/declare/resourceEfficiency",
                        data: JSON.stringify({idList,type:$('#dataType').val()}),
                        dataType: "json",
                        async: false,
                        contentType: "application/json",
                        error: function (request) {
                            parent.layer.alert("Connection error");
                        },
                        success: (data) => {
                            if (data.code == 0) {
                                parent.reLoad();
                                // let myFileData;
                                const xhr = new XMLHttpRequest();
                                xhr.open('get', `/jzweb/declare/fileDownload?idList=` + idList+'&type='+$('#dataType').val());
                                xhr.responseType = 'blob';
                                xhr.send();
                                xhr.onload = function () {
                                    if (this.status === 200 || this.status === 304) {
                                        let name =
                                            xhr.getResponseHeader('Content-Disposition').split('filename=')[1]
                                        name = decodeURI(name)
                                        // 如果是IE10及以上，不支持download属性，采用msSaveOrOpenBlob方法，但是IE10以下也不支持msSaveOrOpenBlob
                                        if ('msSaveOrOpenBlob' in navigator) {
                                            navigator.msSaveOrOpenBlob(this.response, name);
                                            return;
                                        }
                                        // const blob = new Blob([this.response], { type: xhr.getResponseHeader('Content-Type') });
                                        // const url = URL.createObjectURL(blob);
                                        const url = URL.createObjectURL(this.response);
                                        const a = document.createElement('a');
                                        a.style.display = 'none';
                                        a.href = url;
                                        a.download = name;
                                        document.body.appendChild(a);
                                        a.click();
                                        document.body.removeChild(a);
                                        URL.revokeObjectURL(url);
                                        setTimeout( layer.close(loading),1000)
                                    }
                                };
                            } else {
                                parent.layer.alert(data.msg)
                            }
                            layer.close(loading1);
                        }
                    });
                })
            }
        })

    }

    const myExportALlTop=()=>{
        var ids = $('#exampleTable').bootstrapTable('getSelections');
        let idList = []

        if (ids.length == 0) {
            layer.msg("请至少选择一份数据，或者选择全部数据")
            return;
        }
        if (myPageSize == ids.length) {
            console.log("全选")
        } else {
            ids.forEach(item => {
                idList.push(item.id)
            })
            console.log("单独")
        }
        var loading1 = layer.open({
            type: 1,
            area : [ '500px', '400px' ],
            resize: false,
            shadeClose: true,
            title: '分类导出选择条件',
            content: ` <div class="layui-form" style="padding: 20px;">
                                <div class="layui-form-item" style="height: 250px;display: flex;align-items: center;">
                                    <label class="layui-form-label" style="width: 100px;">数据类型</label>
                                      <div class="layui-input-inline " style="width: 350px;">
                                        <select id="dataType" class="layui-input" value="" placeholder="请选择数据类型">
                                        <option value="">全部</option>
                                        <option value="1">实习单位</option>
                                        <option value="2">优秀培训教师</option>
                                        <option value="3">优秀考务工作者</option>
                                        <option value="4">优秀组织管理奖</option>
                                        <option value="5">优秀案例</option>
                                        </select>
                                    </div>
                                </div>
                                 <div class="layui-form-item"> <button class="layui-btn layui-btn-fluid" id="btn2" >确定</button>
                                 </div>
                           </div>`,
            success: function(){
                layui.use('form', function(){
                    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                });
                $('#btn2').click(function(){
                    var loading = layer.load('加载中...');
                    //调接口
                    $.ajax({
                        cache: true,
                        type: "POST",
                        url: "/jzweb/declare/resourceEfficiency",
                        data: JSON.stringify({idList,type:$('#dataType').val()}),
                        dataType: "json",
                        async: false,
                        contentType: "application/json",
                        error: function (request) {
                            parent.layer.alert("Connection error");
                        },
                        success: (data) => {
                            if (data.code == 0) {
                                parent.reLoad();
                                // let myFileData;
                                const xhr = new XMLHttpRequest();
                                xhr.open('get', `/jzweb/declare/exportList?idList=` + idList+'&type='+$('#dataType').val());
                                xhr.responseType = 'blob';
                                xhr.send();
                                xhr.onload = function () {
                                    if (this.status === 200 || this.status === 304) {
                                        let name =
                                            xhr.getResponseHeader('Content-Disposition').split('filename=')[1]
                                        name = decodeURI(name)
                                        // 如果是IE10及以上，不支持download属性，采用msSaveOrOpenBlob方法，但是IE10以下也不支持msSaveOrOpenBlob
                                        if ('msSaveOrOpenBlob' in navigator) {
                                            navigator.msSaveOrOpenBlob(this.response, name);
                                            return;
                                        }
                                        // const blob = new Blob([this.response], { type: xhr.getResponseHeader('Content-Type') });
                                        // const url = URL.createObjectURL(blob);
                                        const url = URL.createObjectURL(this.response);
                                        const a = document.createElement('a');
                                        a.style.display = 'none';
                                        a.href = url;
                                        a.download = name;
                                        document.body.appendChild(a);
                                        a.click();
                                        document.body.removeChild(a);
                                        URL.revokeObjectURL(url);
                                        setTimeout( layer.close(loading),1000)
                                    }
                                };
                                layer.close(loading1);
                            } else {
                                parent.layer.alert(data.msg)
                            }

                        }
                    });
                })
            }
        })
    }
    const myExport = (id)=> {
        var loading1 = layer.open({
            type: 1,
            area : [ '500px', '400px' ],
            resize: false,
            shadeClose: true,
            title: '分类导出选择条件',
            content: ` <div class="layui-form" style="padding: 20px;">
                                <div class="layui-form-item" style="height: 250px;display: flex;align-items: center;">
                                    <label class="layui-form-label" style="width: 100px;">数据类型</label>
                                      <div class="layui-input-inline " style="width: 350px;">
                                        <select id="dataType" class="layui-input" value="" placeholder="请选择数据类型">
                                        <option value="">全部</option>
                                        <option value="1">实习单位</option>
                                        <option value="2">优秀培训教师</option>
                                        <option value="3">优秀考务工作者</option>
                                        <option value="4">特殊贡献奖</option>
                                        <option value="5">优秀案例</option>
                                        </select>
                                    </div>
                                </div>
                                 <div class="layui-form-item"> <button class="layui-btn layui-btn-fluid" id="btn2" >确定</button>
                                 </div>
                           </div>`,
            success: function(){
                layui.use('form', function(){
                    var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
                    form.render();
                });
                $('#btn2').click(function(){
                    var loading = layer.load('加载中...');
                    //调接口
                    $.ajax({
                        cache: true,
                        type: "POST",
                        url: "/jzweb/declare/resourceEfficiency",
                        data: JSON.stringify({idList: [id],type:$('#dataType').val()}),
                        dataType: "json",
                        async: true,
                        contentType: "application/json",
                        error: function (request) {
                            parent.layer.alert("Connection error");
                        },
                        success: (data) => {
                            if (data.code == 0) {
                                parent.reLoad();
                                let idList = [id];
                                const xhr = new XMLHttpRequest();
                                xhr.open('get', `/jzweb/declare/fileDownload?idList=` + idList+'&type='+$('#dataType').val());
                                xhr.responseType = 'blob';
                                xhr.send();
                                xhr.onload = function () {
                                    if (this.status === 200 || this.status === 304) {
                                        let name =
                                            xhr.getResponseHeader('Content-Disposition').split('filename=')[1]
                                        name = decodeURI(name)
                                        // 如果是IE10及以上，不支持download属性，采用msSaveOrOpenBlob方法，但是IE10以下也不支持msSaveOrOpenBlob
                                        if ('msSaveOrOpenBlob' in navigator) {
                                            navigator.msSaveOrOpenBlob(this.response, name);
                                            return;
                                        }
                                        // const blob = new Blob([this.response], { type: xhr.getResponseHeader('Content-Type') });
                                        // const url = URL.createObjectURL(blob);
                                        const url = URL.createObjectURL(this.response);
                                        const a = document.createElement('a');
                                        a.style.display = 'none';
                                        a.href = url;
                                        a.download = name;
                                        document.body.appendChild(a);
                                        a.click();
                                        document.body.removeChild(a);
                                        URL.revokeObjectURL(url);
                                        setTimeout( layer.close(loading),1000)
                                    }
                                };
                            } else {
                                parent.layer.alert(data.msg)
                            }
                            layer.close(loading1);
                        }
                    });
                })
            }
        })


    }

    function infors(id) {
        layer.open({
            type: 2,
            title: '申报详情',
            maxmin: true,
            shadeClose: false, // 点击遮罩关闭层
            area: ['100%', '100%'],
            content: '/jzweb/declarationManagement/inforList/' + id,// iframe的url
            // success: function(layero) {
            //     console.log('layer-anim-00===》》》》',$('.layer-anim').length,$('.layer-anim-00').length)
            //     //处理layer层中video播放器全屏样式问题
            //     setTimeout(function () {
            //         $(layero).removeClass('layer-anim');
            //         $(layero).removeClass('layer-anim-00');
            //         console.log('处理后面=======5555',$('.layer-anim').length,$('.layer-anim-00').length)
            //     }, 0);
            // }
        });
    }


</script>
</body>
</html>